package com.gugee.ins.common.dal.service.blogger.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gugee.ins.common.constant.GugeeInsExceptionConst;
import com.gugee.ins.common.dal.mapper.blogger.InsBloggerMainExtMapper;
import com.gugee.ins.common.dal.service.blogger.InsBloggerMainDao;
import com.gugee.ins.common.model.ai.InComeRequest;
import com.gugee.ins.common.model.ai.InComeResponse;
import com.gugee.ins.common.model.ai.InsBloggerCategory;
import com.gugee.ins.common.model.ai.InsBloggerLanguage;
import com.gugee.ins.common.model.mysql.blogger.InsBlogger;
import com.gugee.ins.common.model.mysql.blogger.InsBloggerMain;
import com.gugee.ins.common.model.spider.blogger.InsBloggerUserFromId;
import com.gugee.ins.data.common.msg.InsBloggerMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.List;

/**
 * insBlogger dao
 * @author Huangxin
 */

@Slf4j
@Service
public class InsBloggerMainDaoImpl extends ServiceImpl<InsBloggerMainExtMapper,InsBloggerMain> implements InsBloggerMainDao {

    @Override
    public long getMaxId() {
        return baseMapper.getMaxId();
    }

    @Override
    public List<InsBloggerMessage> findInsBloggerMessageByPages(long startId, long endId,long followerCount,long lastCollectTime) {
        return baseMapper.findInsBloggerMessageByPages(startId,endId,followerCount,lastCollectTime);
    }
    @Override
    public List<InsBloggerMessage> findInsBloggerMessageByPages(long startId, long endId) {
        return baseMapper.findInsBloggerMessageByPagesId(startId,endId);
    }
    @Override
    public List<InsBloggerMessage> findInsBloggerMessageByPages(long startId, long endId, long followerCount) {
        return baseMapper.findInsBloggerMessageByFollowerCountAndPages(startId,endId,followerCount);
    }

    @Override
    public List<InsBloggerMessage> findInsBloggerMessageByRealFollowerStTime(long startId,long endId,long followerCount,long limitTime) {
        return baseMapper.findInsBloggerMessageByRealFollowerStTime(startId,endId,followerCount,limitTime);
    }

    @Override
    public List<InsBloggerMessage> findInsBloggerMessageByLastGenderStTime(long startId,long endId,long followerCount,long limitTime) {
        return baseMapper.findInsBloggerMessageByLastGenderStTime(startId,endId,followerCount,limitTime);
    }

    @Override
    public int updateMBlogger(InsBloggerUserFromId insBloggerUserFromId) {

        if(insBloggerUserFromId==null ||insBloggerUserFromId.getIsPrivate()==null){
            throw GugeeInsExceptionConst.BIZ_UID_IS_NULL_BLOGGER_MAIN;
        }
        insBloggerUserFromId.setLastCollectTime(System.currentTimeMillis() / 1000);
        return baseMapper.updateByPrimaryKeySelective(insBloggerUserFromId);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public InsBloggerMain findByUid(long uid) {
        return baseMapper.findByUid(uid);
    }

    @Override
    public List<InsBloggerMain> findInUid(List<Long> uids) {
        return baseMapper.findInUid(uids);
    }
    @Override
    public List<InsBloggerMain> produceInsBloggerMainsFromBlogger(List<Long> uidS,int followerCount) {
        return baseMapper.produceInsBloggerMainsFromBlogger(uidS,followerCount,System.currentTimeMillis()/1000);
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchInsertBloggerMains(List<InsBloggerMain> insBloggerMainList) {
        try{
            this.saveBatch(insBloggerMainList);
        }catch (Exception e){
            insBloggerMainList.forEach(insBloggerMain -> {
                try{
                    baseMapper.insert(insBloggerMain);
                }catch (DuplicateKeyException dex){
                    UpdateWrapper<InsBloggerMain> updateWrapper = new UpdateWrapper<>();
                    updateWrapper.eq("uid",insBloggerMain.getUid());
                    try{
                        baseMapper.update(insBloggerMain,updateWrapper);
                    }catch (Exception exception){
                        log.error("update insBloggerMain from insBlogger failed ,uid : {} , msg : {} "
                                ,insBloggerMain.getUid(), exception.getMessage());
                    }
                }catch (Exception ex){
                    log.error("insert insBloggerMain from insBlogger failed ,uid : {} , msg : {} "
                            ,insBloggerMain.getUid(), ex.getMessage());
                }
            });
        }
    }
    @Override
    public double stBMAvgRealFollowerRateByCategoryAndFollower(String category, int followerStart, int followerEnd) {
        return baseMapper.stBMAvgRealFollowerRateByCategoryAndFollower(category,followerStart,followerEnd);
    }

    @Override
    public double stBMAvgFollowerGrowRateByCategoryAndFollower(String category, int followerStart, int followerEnd) {
        return baseMapper.stBMAvgFollowerGrowRateByCategoryAndFollower(category,followerStart,followerEnd);
    }

    @Override
    public double stBMSumMediaErByCategoryAndFollower(String category, int followerStart, int followerEnd,long timeRang,Boolean isAd) {
        return baseMapper.stBMSumMediaErByCategoryAndFollower(category,followerStart,followerEnd,timeRang,isAd);
    }

    @Override
    public long countByCategoryAndFollower(String category, int followerStart, int followerEnd) {
        return baseMapper.countByCategoryAndFollower(category,followerStart,followerEnd);
    }
    /**
     * 大播主收录
     * @param uidS 可能需收录的uid列表
     */
    @Override
    public void collectMBlogger(List<Long> uidS,int followerCount,long createTime) {
        baseMapper.collectMBloggerFromUidS(uidS, followerCount, createTime);
    }

    /**
     * 根据uid条件更新
     * @param insBloggerMain 数据
     * @return
     */
    @Override
    public void updateByUidSelective(InsBloggerMain insBloggerMain){
        UpdateWrapper<InsBloggerMain> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("uid",insBloggerMain.getUid());
        baseMapper.update(insBloggerMain,updateWrapper);
    }

    /**
     * 根据uid 更新年龄和性别
     * @param age
     * @param gender
     * @param birth
     * @param race
     * @param uid
     */
    @Override
    public void updateAiGenderAndAiAgeByUid(int age,int gender,int birth,String race,long uid){
        baseMapper.updateAiGenderAndAiAgeByUid(age,gender,birth,race,uid);
    }

    /**
     * 根据uid 更新国家
     * @param uid
     * @param nation
     * @param nationSource
     */
    @Override
    public void updateNation(long uid,String nation,int nationSource){
        baseMapper.updateNation(uid,nation,nationSource);
    }

    /**
     * 根据uid 更新语言
     * @param uid
     * @param language
     */
    @Override
    public void updateLanguage(long uid,String language){
        baseMapper.updateLanguage(uid,language);
    }

    /**
     * 批量更新 语言
     * @param insBloggerLanguages
     */
    @Override
    public void batchUpdateLanguage(List<InsBloggerLanguage> insBloggerLanguages){
        try{
            baseMapper.batchUpdateLanguage(insBloggerLanguages);
        }catch (Exception e){
            e.printStackTrace();
            log.debug("batch Update ins blogger main  Analysis Language failed ,try Update one by one, msg : {} ",e.getMessage());
            insBloggerLanguages.forEach(insBloggerLanguage -> {
                try{
                    baseMapper.updateLanguage(insBloggerLanguage.getUid(),insBloggerLanguage.getLanguage());
                }catch (Exception ex){
                    e.printStackTrace();
                    log.info("Update ins blogger main Analysis Language failed ,uid : {},language : {} , msg : {} "
                            ,insBloggerLanguage.getUid(),insBloggerLanguage.getLanguage(),ex.getMessage());
                }
            });
        }
    }

    /**
     * 根据uids 更新 近30天粉丝增长数和粉丝增长率
     * @param uids
     */
    @Override
    public void updateLastFollowerGrowByUids(List<Long> uids,int dateCode){
        baseMapper.updateLastFollowerGrowByUids(uids,dateCode);
    }

    /**
     * 根据id分页查询uid
     * @param startId
     * @param endId
     * @param followerCount
     * @return
     */
    @Override
    public List<Long> findInsBloggerUidByPages(long startId, long endId,long followerCount) {
        return baseMapper.findInsBloggerUidByPages(startId,endId,followerCount);
    }

    /**
     * 更新帖子采集时间
     * @param uid
     */
    @Override
    public void updateMediaCollectTime(long uid) {
        baseMapper.updateMediaCollectTime(uid,System.currentTimeMillis() / 1000);
    }

    /**
     * 更新帖子平均参与率
     * @param uid
     * @param timeRang
     */
    @Override
    public void updateAvgEr(long uid,long timeRang) {
        baseMapper.updateAvgEr(uid,timeRang);
    }

    /**
     * 更新评论采集
     * @param uid
     * @param commentCollectCount
     */
    @Override
    public void updateCommentCollectTime(long uid,int commentCollectCount) {
        baseMapper.updateCommentCollectTime(uid,commentCollectCount,System.currentTimeMillis() / 1000);
    }


    /**
     * 更新粉丝列表采集时间
     * @param uid
     */
    @Override
    public void updateFollowerCollectTime(long uid) {
        baseMapper.updateFollowerCollectTime(uid,System.currentTimeMillis() / 1000);
    }

    /**
     * 更新博主广告帖子平均ER值
     * @param uid
     */
    @Override
    public void updateBrandMediaEr(long uid, double brandMediaEr) {
        baseMapper.updateBrandMediaEr(uid,brandMediaEr);
    }

    /**
     * 获取需要采集帖子的播主消息数据
     * @param startId
     * @param endId
     * @param followerCount
     * @param mediaMinCount
     * @param lastMediaCollectTime
     * @return
     */
    @Override
    public List<InsBloggerMessage> findInsMediaCollectMessageByPages(long startId, long endId,long followerCount,
                                                                     int mediaMinCount, long lastMediaCollectTime) {
        return baseMapper.findInsMediaCollectMessageByPages(startId,endId,followerCount,mediaMinCount,lastMediaCollectTime);
    }

    /**
     * 获取需要采集评论的播主消息数据
     * @param startId
     * @param endId
     * @param followerCount
     * @param lastCommentCollectTime
     * @return
     */
    @Override
    public List<InsBloggerMessage> findInsCommentCollectMessageByPages(long startId, long endId,long followerCount,
                                                                long lastCommentCollectTime){
        return baseMapper.findInsCommentCollectMessageByPages(startId,endId,followerCount,lastCommentCollectTime);
    }

    /**
     * 获取需要采集活跃粉丝的播主消息数据
     * @param startId
     * @param endId
     * @param followerCount
     * @param lastFollowerCollectTime
     * @return
     */
    @Override
    public List<InsBloggerMessage> findInsBloggerFollowerCollectMessageByPages(long startId, long endId,long followerCount,
                                                                        long lastFollowerCollectTime){
        return baseMapper.findInsBloggerFollowerCollectMessageByPages(startId,endId,followerCount,lastFollowerCollectTime);
    }

    /**
     * 获取需要分析的帖子消息
     * @param startId
     * @param endId
     * @param followerCount
     * @param mediaMinCount
     * @return
     */
    @Override
    public List<InsBloggerMessage> findAdMediaIdentifyByPages(long startId, long endId, long followerCount,
                                                     int mediaMinCount){
        return baseMapper.findAdMediaIdentifyByPages(startId,endId,followerCount,mediaMinCount);
    }

    /**
     * 获取需要采集地理位置的播主消息数据
     * @param startId
     * @param endId
     * @param followerCount
     * @return
     */
    @Override
    public List<InsBloggerMessage> findInsLocationCollectMessageByPages(long startId, long endId,long followerCount){
        return baseMapper.findInsLocationCollectMessageByPages(startId,endId,followerCount);
    }

    /**
     * 获取需要采集地理位置的大播主活跃粉丝消息数据
     * @param uid
     * @param limitTime
     * @return
     */
    @Override
    public List<InsBlogger> findInsLocationCollectMessageByUid(long uid, long limitTime){
        return baseMapper.findInsLocationCollectMessageByUid(uid,limitTime);
    }

    /**
     * 获取需要采集关注的播主消息数据
     * @param startId
     * @param endId
     * @param followerCount
     * @return
     */
    @Override
    public List<InsBloggerMessage> findInsBloggerFollowingCollectMessageByPages(long startId, long endId,long followerCount){
        return baseMapper.findInsBloggerFollowingCollectMessageByPages(startId,endId,followerCount);
    }

    /**
     * 获取需要采集关注的大播主活跃粉丝消息数据
     * @param uid
     * @param limitTime
     * @param followingMinCount
     * @return
     */
    @Override
    public List<InsBloggerMessage> findInsBloggerFollowingCollectMessageByUid(long uid,long limitTime,int followingMinCount,int limitCount){
        return baseMapper.findInsBloggerFollowingCollectMessageByUid(uid,limitTime,followingMinCount,limitCount);
    }

    /**
     * 获取需要分析头像的播主消息数据
     * @param startId
     * @param endId
     * @param followerCount
     * @return
     */
    @Override
    public List<InsBloggerMessage> findInsAnalysisAvatarMessageByPages(long startId, long endId,long followerCount){
        return baseMapper.findInsAnalysisAvatarMessageByPages(startId,endId,followerCount);
    }

    /**
     * 获取需要分析头像的大播主活跃粉丝消息数据
     * @param uid
     * @param limitTime
     * @return
     */
    @Override
    public List<InsBloggerMessage> findInsAnalysisAvatarMessageByUid(long uid,long limitTime){
        return baseMapper.findInsAnalysisAvatarMessageByUid(uid,limitTime);
    }

    /**
     * 获取需要分析语言的播主消息数据
     * @param startId
     * @param endId
     * @param followerCount
     * @return
     */
    @Override
    public List<InsBloggerMessage> findInsAnalysisLanguageMessageByPages(long startId, long endId,long followerCount){
        return baseMapper.findInsAnalysisLanguageMessageByPages(startId,endId,followerCount);
    }

    /**
     * 获取需要分析国家的播主消息数据
     * @param startId
     * @param endId
     * @param followerCount
     * @return
     */
    @Override
    public List<InsBloggerMessage> findInsAnalysisNationMessageByPages(long startId, long endId,long followerCount){
        return baseMapper.findInsAnalysisNationMessageByPages(startId,endId,followerCount);
    }

    /**
     * 获取需要分析国家的的大播主活跃粉丝消息数据
     * @param uid
     * @param limitTime
     * @return
     */
    @Override
    public List<InsBloggerMessage> findInsAnalysisNationMessageByUid(long uid,long limitTime){
        return baseMapper.findInsAnalysisNationMessageByUid(uid,limitTime);
    }

    /**
     * 获取指定的粉丝量级id信息
     * @param idLimit
     * @return
     */
    @Override
    public List<Integer> findFollowerSizeIdByLimitId(int idLimit){
        return baseMapper.findFollowerSizeIdByLimitId(idLimit);
    }

    /**
     * 获取同分类同量级的播主总数
     * @param category
     * @param followerSize
     * @return
     */
    @Override
    public int findTotalCountByCategoryAndFollowerSize(String category,int followerSize){
        return baseMapper.findTotalCountByCategoryAndFollowerSize(category,followerSize);
    }

    /**
     * 更新真实粉丝占比率
     * @param id
     * @param realFollowerRate
     */
    @Override
    public void updateRealFollowerRate(long id,double realFollowerRate){
        baseMapper.updateRealFollowerRate(id,realFollowerRate,System.currentTimeMillis() / 1000);
    }

    /**
     * 获取需要分析收入的大播主活跃粉丝消息数据
     * @param uid
     * @param limitTime
     * @return
     */
    @Override
    public List<InComeRequest> findInsAnalysisIncomeByUid(long uid, long limitTime){
        return baseMapper.findInsAnalysisIncomeByUid(uid,limitTime);
    }

    /**
     * 获取需要分析收入的大播主消息数据
     * @param uid
     * @return
     */
    @Override
    public List<InComeRequest> findMInsAnalysisIncomeByUid(long uid){
        return baseMapper.findMInsAnalysisIncomeByUid(uid);
    }

    /**
     * 批量更新ai收入
     * @param inComeResponses
     */
    @Override
    public void batchUpdateIncome(List<InComeResponse> inComeResponses){
        baseMapper.batchUpdateIncome(inComeResponses);
    }

    /**
     * 更新活跃粉丝的统计时间字段
     * @param insBloggerMain
     */
    @Override
    public void updateStActiveFollowerTime(InsBloggerMain insBloggerMain){
        baseMapper.updateStActiveFollowerTime(insBloggerMain);
    }

    /**
     * 更新播主平均点赞数和平均评论数
     * @param uid
     * @param limitTime
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateMediaAvg(long uid, long limitTime) {
        baseMapper.updateMediaAvg(uid,limitTime);
    }

    /**
     * 批量更新ai分类
     * @param insBloggerCategories
     */
    @Override
    public void batchUpdateAiCategory(List<InsBloggerCategory> insBloggerCategories) {
        long now = System.currentTimeMillis() / 1000;
        baseMapper.batchUpdateAiCategory(insBloggerCategories,now);
    }

    /**
     * 根据给定的uid 查找出未进行播主分类的uid
     * @param insBloggerMessages
     * @return
     */
    @Override
    public List<Long> findEmptyCategoryByUid(List<InsBloggerMessage> insBloggerMessages) {
        return baseMapper.findEmptyCategoryByUid(insBloggerMessages);
    }

    /**
     * 获取需要分析分类的播主消息数据
     * @param startId
     * @param endId
     * @param followerCount
     * @param limitTime
     * @return
     */
    @Override
    public List<InsBloggerMessage> findInsAnalysisCategoryMessageByPages(long startId, long endId, long followerCount, long limitTime) {
        return baseMapper.findInsAnalysisCategoryMessageByPages(startId,endId,followerCount,limitTime);
    }

    /**
     * 更新最近分析播主分类的时间
     * @param uid
     */
    @Override
    public void updateLastAnalysisCategoryTime(long uid) {
        baseMapper.updateLastAnalysisCategoryTime(uid,System.currentTimeMillis() / 1000);
    }

    /**
     * 获取需要更新数据的掉分的大播主消息数据
     * @param startId
     * @param endId
     * @param followerCount
     * @param limitTime
     * @return
     */
    @Override
    public List<InsBloggerMessage> findUpdateLowFollowerMBloggerMsgByPages(long startId, long endId, long followerCount, long limitTime) {
        return baseMapper.findUpdateLowFollowerMBloggerMsgByPages(startId,endId,followerCount,limitTime);
    }

    /**
     * 获取需要分析收入的播主消息数据
     * @param startId
     * @param endId
     * @param followerCount
     * @return
     */
    @Override
    public List<InsBloggerMessage> findInsAnalysisIncomeMessageByPages(long startId, long endId,long followerCount){
        return baseMapper.findInsAnalysisIncomeMessageByPages(startId,endId,followerCount);
    }

    /**
     * 更新状态
     * @param uid
     * @param status
     */
    @Override
    public void updateAccountStatus(long uid ,int status) {
        baseMapper.updateAccountStatus(uid,status);
    }

    /**
     * 查看是否存在
     * @param uid
     * @return
     */
    @Override
    public boolean findExist(long uid){
        return baseMapper.findExist(uid) != null;
    }


    /**
     * 获取需要更新数据的中播主消息数据
     * @param startId
     * @param endId
     * @param mainBloggerFollowerCount
     * @param bigBloggerFollowerCount
     * @param limitTime
     * @return
     */
    @Override
    public List<InsBloggerMessage> findUpdateMiddleBloggerMsgByPages(long startId, long endId, long mainBloggerFollowerCount,
                                                                     long bigBloggerFollowerCount, long limitTime) {
        return baseMapper.findUpdateMiddleBloggerMsgByPages(startId,endId,mainBloggerFollowerCount,bigBloggerFollowerCount,limitTime);
    }

    /**
     * 获取需要采集帖子的中播主消息数据
     * @param startId
     * @param endId
     * @param mainBloggerFollowerCount
     * @param bigBloggerFollowerCount
     * @param mediaMinCount
     * @param lastMediaCollectTime
     * @return
     */
    @Override
    public List<InsBloggerMessage> findInsMiddleBgMediaCollectMessageByPages(long startId, long endId,long mainBloggerFollowerCount,
                                                                     long bigBloggerFollowerCount,int mediaMinCount,
                                                                     long lastMediaCollectTime) {
        return baseMapper.findInsMiddleBgMediaCollectMessageByPages(startId,endId,mainBloggerFollowerCount,bigBloggerFollowerCount,
                mediaMinCount,lastMediaCollectTime);
    }

    /**
     * 查询需要分析品牌的播主信息
     * @param startId
     * @param endId
     * @param limitTime
     * @return
     */
    @Override
    public List<InsBloggerMessage> findAnalysisBrandBloggerByPages(long startId, long endId, long limitTime) {
        return baseMapper.findAnalysisBrandBloggerByPages(startId,endId,limitTime);
    }

    /**
     * 更新最近分析品牌的标记字段
     * @param uid
     * @param brandJson
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateAiBrand(long uid,String brandJson) {
        baseMapper.updateAiBrand(uid,brandJson,System.currentTimeMillis() / 1000);
    }

    /**
     * 根据uid 获取 userName
     * @param uids
     * @return
     */
    @Override
    public List<String> findUserNameByUid(List<Long> uids){
        return baseMapper.findUserNameByUid(uids);
    }
}
